package com.auto.qinglong.ui.viewmodel

import androidx.lifecycle.viewModelScope
import com.auto.base.datastore.DataStoreUtil
import com.auto.base.net.QlConstant
import com.auto.base.net.ResultWrapper
import com.auto.base.ui.BaseViewModel
import com.auto.qinglong.net.client.QlRepository
import com.auto.qinglong.net.client.beans.LoginBean
import com.auto.qinglong.net.client.beans.QlAccount
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Created by YuanWei1 on 2025/6/12.
 *
 * Description:
 */


data class LoginUiState(val callback: ResultWrapper<LoginBean>?)

data class LoginViewModelState(
    val name: String = "",
    val pwd: String = "",
    val ip: String = "",
    val result: ResultWrapper<LoginBean>? = null
) {
    fun toUiState(): LoginUiState = LoginUiState(callback = result)

}


@HiltViewModel
class LoginViewModel @Inject constructor(
    val dataStoreUtil: DataStoreUtil, val qlRepository: QlRepository
) : BaseViewModel() {
    private val _viewModelState = MutableStateFlow(
        LoginViewModelState()
    )
    val viewModelState: StateFlow<LoginViewModelState> = _viewModelState.asStateFlow()
    val uiState = viewModelState.map {
        it.toUiState()
    }.stateIn(
        viewModelScope, SharingStarted.Eagerly, viewModelState.value.toUiState()
    )

    init {
        getNamePwd()
    }

    private fun getNamePwd() {
        viewModelScope.launch {
            val name = dataStoreUtil.getData(DataStoreUtil.Key.NAME, "13673921628").first()
            val pwd = dataStoreUtil.getData(DataStoreUtil.Key.PWD, "2916893").first()
            val ip = dataStoreUtil.getData(DataStoreUtil.Key.IP, "192.168.31.234").first()
            QlConstant.Ip = ip
            _viewModelState.update {
                it.copy(name = name, pwd = pwd, ip = ip)
            }
        }
    }

    fun saveIp() {
        QlConstant.Ip = viewModelState.value.ip
    }

    fun login(success: () -> Unit = {}) {
        setLoading(true)
        val temp = viewModelState.value
        viewModelScope.launch {
            val result = qlRepository.login(QlAccount(temp.name, temp.pwd))
            if (result is ResultWrapper.Success) {
                result.data?.let {
                    var temp = it.token
                    if (!temp.startsWith("Bearer ")) {
                        temp = "Bearer $temp";
                    }
                    QlConstant.token = temp
                    _viewModelState.update { state ->
                        state.copy(result = result)
                    }
                }
            }
        }
    }


    fun updateUserName(newName: String) {
        _viewModelState.update { it.copy(name = newName) }
    }

    fun updatePwd(pwd: String) {
        _viewModelState.update { it.copy(pwd = pwd) }
    }

    fun updateIp(ip: String) {
        _viewModelState.update { it.copy(ip = ip) }
    }

    fun saveNamePwd() {
        viewModelScope.launch {
            dataStoreUtil.saveData(DataStoreUtil.Key.NAME, viewModelState.value.name)
            dataStoreUtil.saveData(DataStoreUtil.Key.PWD, viewModelState.value.pwd)
            dataStoreUtil.saveData(DataStoreUtil.Key.IP, viewModelState.value.ip)
        }
    }
}